package swing.admin.views.aside;

import swing.admin.common.AdminContextHolder;
import swing.admin.common.Constant;
import swing.admin.views.main.AdminMain;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

public class AdminAside extends JPanel {

    private AdminMain main;

    private List<AsideMenuItem> menuItemList= new ArrayList<>();


    public AdminAside(AdminMain main) {
        this.main = main;
        this.setBackground(new Color(250, 250, 250));
        this.setPreferredSize(new Dimension(Constant.Aside.DEFAULT_ASIDE_WIDTH, Constant.Aside.DEFAULT_ASIDE_HEIGHT));
        this.initMenuData();
        add(createPanel());
    }


    private void initMenuData() {
        AdminContextHolder.getContext().getMenuCardList().forEach(card ->{
            AsideMenuItem menuItem = new AsideMenuItem(card.getMenuName(), this.menuItemList);
            menuItem.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    switchCard(menuItem.name);
                }
            });
            this.menuItemList.add(menuItem);
        });
    }

    private void switchCard(String name) {
        CardLayout cardLayout = (CardLayout) main.getLayout();
        cardLayout.show(main, name);
    }

    private JPanel createPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        menuItemList.forEach(panel::add);
        return panel;
    }
}
